package logic

import (
	"context"
	"wxapi/internal/services"
	"wxapi/internal/svc"
	"wxapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AppointmentBySymptomLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAppointmentBySymptomLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppointmentBySymptomLogic {
	return &AppointmentBySymptomLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AppointmentBySymptomLogic) AppointmentBySymptom(req *types.AppointmentBySymptomReq) (resp *types.AppointmentBySymptomResp, err error) {
	skillList, err := services.GetSkillBySymptom(l.ctx, l.svcCtx, req.SymptomId)
	if err != nil {
		return nil, err
	}
	if len(skillList) == 0 {
		return nil, nil
	}
	skillIdList := make([]int64, 0)
	for _, v := range skillList {
		skillIdList = append(skillIdList, v.ID)
	}

	doctorList, err := services.GetDoctorBySkill(l.ctx, l.svcCtx, req.StoreId, skillIdList...)
	if err != nil {
		return nil, err
	}

	resp = &types.AppointmentBySymptomResp{
		Doctor: make([]types.AppointmentDoctor, 0),
	}
	for _, doctor := range doctorList {
		d := types.AppointmentDoctor{
			ID:     doctor.ID,
			Name:   doctor.DoctorName,
			Avatar: doctor.DoctorAvatar,
			Level:  doctor.Level.DoctorLevelName,
			Point:  doctor.SellPoint.Point,
			Skill:  make([]types.AppointmentDoctorSkill, 0),
		}
		for _, skill := range doctor.Skills {
			d.Skill = append(d.Skill, types.AppointmentDoctorSkill{
				ID:       skill.ID,
				Name:     skill.Skill.SkillName,
				Duration: skill.Duration,
				Price:    skill.Price.InexactFloat64(),
			})
		}

		resp.Doctor = append(resp.Doctor, d)
	}
	return
}
